#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
typedef pair<LL,LL>P;
struct edge{
    LL to,cost;
};
vector<edge>g[5000];
LL dis[5000];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool vis[5000];
char str[110][110];
LL n,m;
LL dijkstra(LL st){
    LL ans=-1;
    LL s=st;
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    priority_queue< P, vector<P> , greater<P> >que;
    que.push({0,s});
    while(!que.empty()){
        P now=que.top();que.pop();
        LL u=now.second;
        if(vis[u]) continue;
        vis[u]=true;
        for(LL i=0;i<g[u].size();i++){
            edge e=g[u][i];
            LL v=e.to;LL fee=e.cost;
            if(dis[v]>dis[u]+fee){
                dis[v]=dis[u]+fee;
                que.push({dis[v],v});
            }
        }
    }
    for(LL i=1;i<=n;i++){
        for(LL j=1;j<=m;j++){
            ans=max(ans,dis[(i-1)*m+j]+(str[i][j]=='B'));
        }
    }
    return ans;
}
int main(void)
{
 /// freopen("5.in","r",stdin);
 /// freopen("mysolve.out","w",stdout);

  n=read();m=read();

  for(LL i=1;i<=n;i++){
        scanf("%s",str[i]+1);
  }
  if(n==1&&m==1){
    if(str[1][1]=='B') printf("1\n");
    else printf("0\n");
    return 0;
  }
  for(LL i=1;i<=n;i++){
    for(LL j=1;j<=m;j++){
        for(LL k=0;k<4;k++){
            LL xx=i+dx[k];LL yy=j+dy[k];
            if(xx<1||xx>n||yy<1||yy>m) continue;
            g[(i-1)*m+j].push_back({(xx-1)*m+yy,(str[i][j]==str[xx][yy]?0:1)});
            g[(xx-1)*m+yy].push_back({(i-1)*m+j,(str[i][j]==str[xx][yy]?0:1)});
        }
    }
  }
  /*
  for(LL i=1;i<=n*m;i++){
    for(auto j:g[i]){
        cout<<j.to<<" "<<j.cost<<" ";
    }
    cout<<"\n";
  }*/

  LL sum=0x3f3f3f3f;
  for(LL i=1;i<=n*m;i++){
    LL t=dijkstra(i);
    sum=min(sum,t);
  }
  printf("%d\n",sum);
return 0;
}
